#pragma once

#include<iostream>

using namespace std;

namespace byh
{
	template<class T>
	struct ListNode
	{
		ListNode(const T& val = T())
			:_next(nullptr)
			,_prev(nullptr)
			,_val(val)
		{}

		ListNode<T>* _next;
		ListNode<T>* _prev;
		T _val;
	};

	template<class T, class Ref, class Ptr>
	struct ListIterator
	{
		typedef ListNode<T> Node;

		ListIterator(Node* node)
			:_node(node)
		{}

		Ref operator*()
		{
			return _node->_val;
		}

		Ptr operator->()
		{
			return &this->_node->_val;
		}

		// ++it
		ListIterator& operator++()
		{
			_node = _node->_next;
			return *this;
		}

		// it++
		ListIterator operator++(int)
		{
			ListIterator temp = *this;
			_node = _node->_next;
			return temp;
		}

		// --it
		ListIterator& operator--()
		{
			_node = _node->_prev;
			return *this;
		}

		// it--
		ListIterator operator--(int)
		{
			ListIterator temp = *this;
			_node = _node->_prev;
			return temp;
		}

		bool operator!=(const ListIterator& li)
		{
			return _node != li._node;
		}

		bool operator==(const ListIterator& li)
		{
			return _node == li._node;
		}

		Node* _node;
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;

	public:
		typedef ListIterator<T, T&, T*> iterator;
		typedef ListIterator<T, const T&, const T*> const_iterator;

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}

		list()
		{
			empty_init();
		}

		// li1(li2)
		list(const list<T>& li)
		{
			empty_init();
			for (auto& e : li)
			{
				push_back(e);
			}
		}

		// li1 = li2;
		list<T>& operator=(list<T> li)
		{
			swap(li);
			return *this;
		}

		void swap(list<T> li)
		{
			std::swap(_head, li._head);
			std::swap(_size, li._size);
		}

		iterator begin()
		{
			return _head->_next;
		}

		iterator end()
		{
			return _head;
		}

		const_iterator begin() const
		{
			return _head->_next;
		}

		const_iterator end() const
		{
			return _head;
		}

		void push_back(const T& val)
		{
			insert(end(), val);
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		void insert(iterator pos, const T& val)
		{
			Node* newNode = new Node(val);
			Node* prev = pos._node->_prev;
			prev->_next = newNode;
			newNode->_prev = prev;
			newNode->_next = pos._node;
			pos._node->_prev = newNode;
			_size++;
		}

		iterator erase(iterator pos)
		{
			Node* prev = pos._node->_prev;
			Node* next = pos._node->_next;
			prev->_next = next;
			next->_prev = prev;
			delete pos._node;
			_size--;
			return next;
		}

		bool empty() const
		{
			return _size == 0;
		}

		size_t size() const
		{
			return _size;
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}

		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
			_size = 0;
		}

	private:
		Node* _head;
		size_t _size;
	};

	struct A
	{
		A(int a1 = 0, int a2 = 0)
			:_a1(a1)
			,_a2(a2)
		{}

		int _a1;
		int _a2;
	};

	void Print_list(const list<int>& cli)
	{
		typename list<int>::const_iterator it = cli.begin();
		while (it != cli.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	void Test_list()
	{
		list<int> li;
		li.push_back(1);
		li.push_back(2);
		li.push_back(3);
		li.push_back(4);
		list<int> li2(li);
		list<int> li3 = li;
		Print_list(li3);
	}
}